<?php
/**
 * 数据库配置样例文件
 * 请复制此文件为 database.php 并修改相应配置
 */

// 数据库配置
define('DB_HOST', 'localhost');        // 数据库主机
define('DB_USER', 'your_username');    // 数据库用户名
define('DB_PASS', 'your_password');    // 数据库密码
define('DB_NAME', 's6814949');     // 数据库名称

try {
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4", DB_USER, DB_PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}

// 创建数据库表
function createTables($pdo) {
    $sql = "
    -- 用户表
    CREATE TABLE IF NOT EXISTS users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) UNIQUE NOT NULL,
        password VARCHAR(255) NOT NULL,
        balance DECIMAL(10,2) DEFAULT 1000.00,
        frozen_profit DECIMAL(10,2) DEFAULT 0.00,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

    -- 游戏轮次表
    CREATE TABLE IF NOT EXISTS game_rounds (
        id INT AUTO_INCREMENT PRIMARY KEY,
        round_number INT NOT NULL,
        status ENUM('trading', 'settlement', 'break') DEFAULT 'trading',
        start_time TIMESTAMP NOT NULL,
        end_time TIMESTAMP NOT NULL,
        settlement_price DECIMAL(8,2) DEFAULT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

    -- 价格历史表
    CREATE TABLE IF NOT EXISTS price_history (
        id INT AUTO_INCREMENT PRIMARY KEY,
        round_id INT,
        price DECIMAL(8,2) NOT NULL,
        timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (round_id) REFERENCES game_rounds(id)
    );

    -- 用户持仓表
    CREATE TABLE IF NOT EXISTS user_positions (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        round_id INT NOT NULL,
        round_number INT NOT NULL,
        quantity DECIMAL(10,4) NOT NULL,
        purchase_price DECIMAL(8,2) NOT NULL,
        purchase_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        is_active BOOLEAN DEFAULT TRUE,
        FOREIGN KEY (user_id) REFERENCES users(id),
        FOREIGN KEY (round_id) REFERENCES game_rounds(id)
    );

    -- 交易记录表
    CREATE TABLE IF NOT EXISTS transactions (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        round_id INT NOT NULL,
        type ENUM('buy', 'sell') NOT NULL,
        quantity DECIMAL(10,4) NOT NULL,
        price DECIMAL(8,2) NOT NULL,
        amount DECIMAL(10,2) NOT NULL,
        profit DECIMAL(10,2) DEFAULT 0.00,
        transaction_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (user_id) REFERENCES users(id),
        FOREIGN KEY (round_id) REFERENCES game_rounds(id)
    );

    -- 每日盈利记录表（用于次日解冻）
    CREATE TABLE IF NOT EXISTS daily_profits (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        profit_amount DECIMAL(10,2) NOT NULL,
        earned_date DATE NOT NULL,
        is_available BOOLEAN DEFAULT FALSE,
        available_date DATE NOT NULL,
        FOREIGN KEY (user_id) REFERENCES users(id)
    );
    ";

    try {
        $pdo->exec($sql);
        return true;
    } catch(PDOException $e) {
        error_log("创建表失败: " . $e->getMessage());
        return false;
    }
}

// 初始化数据库
createTables($pdo);
?>
